/**
 * 
 */
package uk.ac.ebi.curtain.utils.file;

import java.io.File;
import java.io.Serializable;

import uk.ac.ebi.curtain.utils.data.FileType;
import uk.ac.ebi.curtain.utils.data.ReadType;
import uk.ac.ebi.curtain.utils.data.FileType.UniversalEntryMapper;

/**
 * @author mhaimel
 *
 */
public class FileInfo implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5795445918132848842L;
	private final File file;
	private final FileType ftype;
	private final ReadType rtype;
	
	public FileInfo(String file, FileType ftype, ReadType rtype) {
		this(new File(file),ftype,rtype);
	}
	
	public FileInfo(File file, FileType ftype, ReadType rtype) {
		super();
		this.file = file;
		this.ftype = ftype;
		this.rtype = rtype;
	}
	public File getFile() {
		return file;
	}
	public FileType getFileType() {
		return ftype;
	}
	public ReadType getReadType() {
		return rtype;
	}	
	

	public String getName(){
		return getFile().getName();
	}
	
	
	public String getNameWithoutFilePostfix(){
		return getFileType().removePostfix(getFile().getName());
	}	
	
	private static class SplitEntryMapper implements EntryMapper{

		private final UniversalEntryMapper[] arr;
		private volatile int cnt = 0;

		public SplitEntryMapper(UniversalEntryMapper[] arr) {
			this.arr = arr;
			cnt = arr.length;
		}

		@Override
		public void mapEntry(EntryMapperBean bean) {
			arr[cnt%getLength()].mapEntry(bean);
			++cnt;
		}
		private int getLength(){
			return arr.length;
		}
		
	}
	
	
	
	@Override
	public String toString() {
		return file + " ["+ftype+"]["+rtype+"]";
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((file == null) ? 0 : file.hashCode());
		result = prime * result + ((ftype == null) ? 0 : ftype.hashCode());
		result = prime * result + ((rtype == null) ? 0 : rtype.hashCode());
		return result;
	}
	
	public void parse(EntryMapper mapper){
		getFileType().getParser().parse(this, mapper);
	}
	
	
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		FileInfo other = (FileInfo) obj;
		if (file == null) {
			if (other.file != null)
				return false;
		} else if (!file.equals(other.file))
			return false;
		if (ftype == null) {
			if (other.ftype != null)
				return false;
		} else if (!ftype.equals(other.ftype))
			return false;
		if (rtype == null) {
			if (other.rtype != null)
				return false;
		} else if (!rtype.equals(other.rtype))
			return false;
		return true;
	}
}
